package com.yuelan.apartment.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @Author: ZhaoYi
 * @date: 24-7-5 9:37
 */
@Configuration
public class ThreadPoolConfig {

    @Bean("updateBillOverdueExecutor")
    public ThreadPoolExecutor threadPoolExecutor() {
        // 核心线程数
        int corePoolSize = 4;
        // 最大线程数
        int maximumPoolSize = 8;
        // 空闲线程的存活时间
        long keepAliveTime = 1L;
        // 存活时间单位
        TimeUnit unit = TimeUnit.MINUTES;
        // 任务队列
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(100);

        return new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                unit,
                workQueue,
                (r, executors) -> {
                    // 自定义的拒绝策略
                    System.err.println("Rejected execution of " + r.toString() +
                            " by " + Thread.currentThread().getName());
                    throw new RuntimeException("Task " + r +
                            " rejected from " + executors.toString());
                });
    }


}
